package com.nwpu.main;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;

/**
 * ShowSessionListTag
 *
 * @author 龙虹宇
 * @version 1.0
 * @date 2023/11/8
 */
public class ShowSessionListTag extends SimpleTagSupport {
    @Override
    public void doTag() throws JspException, IOException {
        // 获取标准输出
        JspWriter out = getJspContext().getOut();
        // 获取pageContext
        PageContext pageContext = (PageContext) getJspContext();
        // 获取sessionList
        String sessionList = (String) pageContext.getSession().getAttribute("sessionList");
        // 获取选择的colors
        String[] colors = sessionList.split(",");
        // 统计各个颜色的数量
        int[] counts = {0, 0, 0};
        String[] colorList = {"light", "brown", "amber"};
        for (int i = 0; i < colors.length; i++) {
            if (colorList[0].equals(colors[i].trim())) {
                counts[0]++;
            } else if (colorList[1].equals(colors[i].trim())) {
                counts[1]++;
            } else if (colorList[2].equals(colors[i].trim())) {
                counts[2]++;
            }
        }

        // 输出到标准输出流
        for (int i = 0; i < 3; i++) {
            out.print("<hr>");
            out.print(colorList[i] + " " + counts[i]);
        }
    }
}
